import { getCache, setCache } from '@/utils';
import { defineStore } from 'pinia';
import API_CONFIG from '@/api/config';
import API_USER from '@/api/user';

export const useUserStore = defineStore('user', {
    state: () => ({
        userInfo: getCache('userInfo'),
        tabActive: 0,
        config: getCache('config'),
        token: getCache('token')
    }),
    actions: {
        setUserInfo() {
            API_USER.getUserInfo().then((res) => {
                this.userInfo = res;
                setCache('userInfo', res);
            })
        },
        setTabActive(index: number) {
            this.tabActive = index;
            setCache('tabActive', index);
        },
        getConfig() {
            let urls = ''
            // #ifdef H5
            urls = location.href.split('?')[0];
            // #endif
            API_CONFIG.getMergeConfig({url:urls}).then((res) => {
                console.log(res);
                this.config = res;
                setCache('config', res);
            })
        },
        async wetchatLogin(e:any) {
            const res:any = await  API_CONFIG.userLogin(e);
            this.token = res.token;
            setCache('token',this.token)
            this.setUserInfo();
        },
    },
    getters: {
        getUserInfo: (state) => state.userInfo,
        getTabActive: (state) => state.tabActive
    },

})
